package com.wang.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class UpDemo {
	 public static void main(String[] args) throws IOException {
	        String filename = "D:\\tmp\\mytest.zip";
	        String path = "D:\\tmp";
	        UpDemo.unZip(filename, path);
	    }


	    public static void unZip(String sourceFilename, String targetDir) throws IOException {
	        unZip(new File(sourceFilename), targetDir);
	    }

	    /**
	     * 将sourceFile解压到targetDir
	     * @param sourceFile
	     * @param targetDir
	     * @throws RuntimeException
	     */
	    public static void unZip(File sourceFile, String targetDir) throws IOException {
	    	//获取解压开始时间
	        long start = System.currentTimeMillis();
	        if (!sourceFile.exists()) {
	            throw new FileNotFoundException("cannot find the file = " + sourceFile.getPath());
	        }
	        ZipFile zipFile = null;
	        try{
	        	//根据原始文件创建一个zip对象
	            zipFile = new ZipFile(sourceFile);
	            Enumeration<?> entries = zipFile.entries();
	            while (entries.hasMoreElements()) {
	                ZipEntry entry = (ZipEntry) entries.nextElement();
	                if (entry.isDirectory()) {
	                    String dirPath = targetDir + "/" + entry.getName();
	                    createDirIfNotExist(dirPath);
	                } else {
	                    File targetFile = new File(targetDir + "/" + entry.getName());
	                    createFileIfNotExist(targetFile);
	                    InputStream is = null;
	                    FileOutputStream fos = null;
	                    try {
	                        is = zipFile.getInputStream(entry);
	                        fos = new FileOutputStream(targetFile);
	                        int len;
	                        byte[] buf = new byte[1024];
	                        while ((len = is.read(buf)) != -1) {
	                            fos.write(buf, 0, len);
	                        }
	                    }finally {
	                        try{
	                            fos.close();
	                        }catch (Exception e){
//	                            log.warn("close FileOutputStream exception", e);
	                        	e.printStackTrace();
	                        }
	                        try{
	                            is.close();
	                        }catch (Exception e){
//	                            log.warn("close InputStream exception", e);
	                        	e.printStackTrace();
	                        }
	                    }
	                }
	            }
	            System.out.println("解压完成，耗时：" + (System.currentTimeMillis() - start) +" ms");
	        } finally {
	            if(zipFile != null){
	                try {
	                    zipFile.close();
	                } catch (IOException e) {
//	                    log.warn("close zipFile exception", e);
	                	e.printStackTrace();
	                }
	            }
	        }
	    }

	    /**
	     * 如果文件夹不存在就创建文件夹（接收一个文件夹路径）
	     * @param path
	     */
	    public static void createDirIfNotExist(String path){
	        File file = new File(path);
	        createDirIfNotExist(file);
	    }

	    /**
	     * 创建文件夹（接收文件对象，方法重载）
	     * @param file
	     */
	    public static void createDirIfNotExist(File file){
	        if(!file.exists()){
	            file.mkdirs();
	        }
	    }
	    
	    /**
	     * 创建文件（接收一个文件对象）
	     * @param file
	     * @throws IOException
	     */
	    public static void createFileIfNotExist(File file) throws IOException {
	        createParentDirIfNotExist(file);
	        file.createNewFile();
	    }
	    
	    /**
	     * 创建文件的父级目录
	     * @param file
	     */
	    public static void createParentDirIfNotExist(File file){
	        createDirIfNotExist(file.getParentFile());
	    }
}
